import { combineReducers } from 'redux'
import handleActions from '../libs/handleActions'

// ------------------------------------
// Constants
// ------------------------------------

export const ERROR_MODAL_SHOW = 'ERROR_MODAL_SHOW'
export const ERROR_MODAL_HIDE = 'ERROR_MODAL_HIDE'

export const ERROR_ALERT_SHOW = 'ERROR_ALERT_SHOW'
export const ERROR_ALERT_HIDE = 'ERROR_ALERT_HIDE'

export const MODAL_SHOW = 'MODAL_SHOW'
export const MODAL_HIDE = 'MODAL_HIDE'

// ------------------------------------
// Actions
// ------------------------------------

// show/hide error modal
export const errorModal = payload => ({
  type: ERROR_MODAL_SHOW,
  payload,
})

export const errorModalHide = () => ({
  type: ERROR_MODAL_HIDE,
})

// show/hide error errorAlert
export const errorAlert = payload => ({
  type: ERROR_ALERT_SHOW,
  payload,
})

export const errorAlertHide = () => ({
  type: ERROR_ALERT_HIDE,
})

// show/hide modal
export const modal = (view, data) => ({
  type: MODAL_SHOW,
  payload: { view, data },
})

export const modalHide = () => ({
  type: MODAL_HIDE,
})

// ------------------------------------
// Reducers
// ------------------------------------

const errorModalReducer = handleActions(
  {
    [ERROR_MODAL_SHOW]: (state, { payload }) => payload,
    [ERROR_MODAL_HIDE]: () => false,
  },
  false,
)

const errorAlertReducer = handleActions(
  {
    [ERROR_ALERT_SHOW]: (state, { payload }) => payload,
    [ERROR_ALERT_HIDE]: () => false,
  },
  false,
)

const modalReducer = handleActions(
  {
    [MODAL_SHOW]: (state, { payload }) => payload,
    [MODAL_HIDE]: () => false,
  },
  false,
)

export default combineReducers({
  errorModal: errorModalReducer,
  errorAlert: errorAlertReducer,
  modal: modalReducer,
})
